import { _decorator, Component, Node, CameraComponent, Prefab, RigidBodyComponent, v3, Vec3 } from "cc";
import { GameMapCtrl } from "./map/MapCtrl";
import { Manager } from "../manager/Manager";
import { ActorController } from "../../resources/prefabs/common/actor/ActorController";
import { Player } from "./entity/player/Player";
const { ccclass, property } = _decorator;

@ccclass("Launch")
export class Launch extends Component {
    @property(CameraComponent)
    public camera: CameraComponent = null;

    @property(Prefab)
    public actorControllerPrefab: Prefab = null;

    private _GameMapCtrl = new GameMapCtrl();
    private _CameraOffset = v3();
    private _Player: Node = null;
    private _ActorController: ActorController = null;

    onLoad(){

    }

    start () {
        this._GameMapCtrl.createGameMap(0, this.node as Node);
        this.initPlayer();
    }

    initPlayer() {
        Manager.Res.getPrefab("prefabs/player/Player").then((prefab: Prefab) => {
            let node = cc.instantiate(prefab) as Node;
            let x = 0;
            let y = 2;
            let z = 0;
            node.setPosition(x, y, -z);
            this.node.addChild(node);
            // let rigiBody = node.getComponent(RigidBodyComponent);
            // rigiBody.setLinearVelocity(v3(1, 0, 0));
            // rigiBody.linearDamping = 0;
            Vec3.subtract(this._CameraOffset, this.camera.node.getWorldPosition(), node.getWorldPosition());
            this._Player = node;
            this._Player.addComponent(Player);
            this.initPlayerController();
        });
    }

    // initPlayerController(){
    //     this._ActorController = cc.instantiate(this.actorControllerPrefab).getComponent(ActorController);
    //     this.node.addChild(this._ActorController.node);
    //     cc.log(this._ActorController);
    //     this._ActorController.setTouchHandler(() => {
    //     });
    // }
    initPlayerController() {
        let loadControllerPrefabFinish = (prefab: Prefab) => {
            let playerController = cc.instantiate(prefab).getComponent(ActorController);
            this.node.addChild(playerController.node);
            // let rigiBody = this._Player.getComponent(RigidBodyComponent);
            playerController.setTouchHandler(() => {
                // rigiBody.isKinematic = false;
                // rigiBody.setLinearVelocity(v3(0, 0, -2));
                this._Player.getComponent(Player).setIsClick(true);
            },
                () => {
                    // let moveDt = playerController.getTouchMoveDelta();
                    // let vNormalize = moveDt.normalize();
                    // rigiBody.setLinearVelocity(v3(vNormalize.x, 0, -vNormalize.y));
                    // this._Player.translate(v3(0.001, 0 ,0));
                }, () => {
                    // rigiBody.setLinearVelocity(v3(-2, 0, 0));
                    // cc.log(rigiBody.linearDamping);
                    // rigiBody.isKinematic = true;
                    this._Player.getComponent(Player).setIsClick(false);
                });
        }
        Manager.Res.getPrefab("prefabs/common/actor/ActorController").then(loadControllerPrefabFinish);
    }


    update (deltaTime: number) {
        if (this._Player){
            let pos = this._Player.getWorldPosition().add(this._CameraOffset);
            this.camera.node.setWorldPosition(pos);
        }
    }
}
